#!/bin/sh
#
# Shell script to update MySQL tables from version 1.36 to 1.38  
#
echo " "
echo "This script will update a Bacula MySQL database from version 8 to 9"
echo "Depending on the size of your database,"
echo "this script may take several minutes to run."
echo " "
bindir=/usr/bin

if $bindir/mysql $*  <<END-OF-DATA
USE bacula;

ALTER TABLE Media ADD COLUMN LabelType INTEGER UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE Media ADD COLUMN StorageId INTEGER UNSIGNED DEFAULT 0;
ALTER TABLE Media ADD COLUMN VolParts INTEGER UNSIGNED NOT NULL DEFAULT 0;

ALTER TABLE Pool  ADD COLUMN LabelType INTEGER UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE Pool  ADD COLUMN NextPoolId INTEGER UNSIGNED DEFAULT 0;
ALTER TABLE Pool  ADD COLUMN MigrationHighBytes BIGINT UNSIGNED DEFAULT 0;
ALTER TABLE Pool  ADD COLUMN MigrationLowBytes BIGINT UNSIGNED DEFAULT 0;
ALTER TABLE Pool  ADD COLUMN MigrationTime BIGINT UNSIGNED DEFAULT 0;

ALTER TABLE JobMedia ADD COLUMN Copy INTEGER UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE JobMedia ADD COLUMN Stripe INTEGER UNSIGNED NOT NULL DEFAULT 0;


CREATE TABLE MediaType (
   MediaTypeId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
   MediaType VARCHAR(128) NOT NULL,
   ReadOnly TINYINT DEFAULT 0,
   PRIMARY KEY(MediaTypeId)
   );

CREATE TABLE Storage (
   StorageId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
   Name VARCHAR(128) NOT NULL,
   AutoChanger TINYINT DEFAULT 0,
   PRIMARY KEY(StorageId)
   );

CREATE TABLE Device (
   DeviceId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
   Name VARCHAR(128) NOT NULL,
   MediaTypeId INTEGER UNSIGNED NOT NULL,
   StorageId INTEGER UNSIGNED,
   DevMounts INTEGER UNSIGNED DEFAULT 0,
   DevReadBytes BIGINT UNSIGNED DEFAULT 0,
   DevWriteBytes BIGINT UNSIGNED DEFAULT 0,
   DevReadBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,
   DevWriteBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,
   DevReadTime BIGINT UNSIGNED DEFAULT 0,
   DevWriteTime BIGINT UNSIGNED DEFAULT 0,
   DevReadTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,
   DevWriteTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,
   CleaningDate DATETIME DEFAULT 0,
   CleaningPeriod BIGINT UNSIGNED DEFAULT 0,
   PRIMARY KEY(DeviceId)
   );

CREATE TABLE Status (
   JobStatus CHAR(1) BINARY NOT NULL,
   JobStatusLong BLOB, 
   PRIMARY KEY (JobStatus)
   );

INSERT INTO Status (JobStatus,JobStatusLong) VALUES
   ('C', 'Created, not yet running'),
   ('R', 'Running'),
   ('B', 'Blocked'),
   ('T', 'Completed successfully'),
   ('E', 'Terminated with errors'),
   ('e', 'Non-fatal error'),
   ('f', 'Fatal error'),
   ('D', 'Verify found differences'),
   ('A', 'Canceled by user'),
   ('F', 'Waiting for Client'),
   ('S', 'Waiting for Storage daemon'),
   ('m', 'Waiting for new media'),
   ('M', 'Waiting for media mount'),
   ('s', 'Waiting for storage resource'),
   ('j', 'Waiting for job resource'),
   ('c', 'Waiting for client resource'),
   ('d', 'Waiting on maximum jobs'),
   ('t', 'Waiting on start time'),
   ('p', 'Waiting on higher priority jobs');


DELETE FROM Version;
INSERT INTO Version (VersionId) VALUES (9);

END-OF-DATA
then
   echo "Update of Bacula MySQL tables succeeded."
else
   echo "Update of Bacula MySQL tables failed."
fi
exit 0
